package com.xky.controller;

import com.xky.pojo.Dept;
import com.xky.pojo.Result;
import com.xky.pojo.UpdateBm;
import com.xky.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j // 日志
public class DeptController {
@Autowired
    private  DeptService deptService;

    /**
     * 查询全部部门
     * @return
     */
    @GetMapping("/depts") // 请求类型
    public Result list() {
        log.info("查询全部部门数据");
        List<Dept> deptList = deptService.list1();
        return Result.success(deptList);
   }

   @GetMapping("/depts/{id}")
   public Result selectid(@PathVariable Integer id){
       log.info("已查询到id为{}的部门", id);
    Dept dept = deptService.selectid(id);
    return Result.success(dept);
   }
    /**
     * 删除部门
     * @param id
     * @return
     */
    @DeleteMapping("/depts/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除部门，部门 ID：{}", id);
        deptService.delete(id);
        return Result.success();
    }

    /**
     * 新增部门
     * @param dept
     * @return
     */
    @PostMapping("/depts")
    public Result insert(@RequestBody Dept dept) {
        try {
            log.info("新增部门{}", dept);
            deptService.add(dept);
            return Result.success();
        } catch (Exception e) {
            log.error("新增部门失败", e);
            return Result.error("新增部门失败，请稍后重试");
        }
    }

    @PutMapping("/depts")
    public  Result update(@RequestBody UpdateBm updateBm){
        Integer id = updateBm.getId();
        String name = updateBm.getName();
        log.info("修改id为{}姓名为{}的部门",id,name);
     UpdateBm updateBm1 = deptService.update(updateBm);
      return Result.success(updateBm1);
    }
}
